<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <p v-html="msg"></p>
        <p v-html="info"></p>
        <p v-html="num"></p>
        <p v-html="msg"></p>
        <p v-html="info"></p>
        <p v-html="num"></p>
        <p v-html="msg"></p>
        <p v-html="info"></p>
        <p v-html="num"></p>
        <hr>
        <input type="button" value="setMsg" vOnClick="setMsg">
    </div>
    <script>
        let Mvvm = {}
        // Options Api => 选项式Api
        Mvvm.createApp = function( options ){
            // 1、默认配置项合并操作
            let defaultOptions = {
                data(){
                    return {}
                },
                methods:{}
            }
            // 对象合并方法，由后一个对象填充覆盖前一个对象
            options = Object.assign( defaultOptions,options );
            // console.log(options);
            return {
                mount:function(el){
                    let root = document.querySelector(el);
                    // 2. 基于定义的工厂函数构建出新的数据存储对象
                    let _data = options.data();
                    // console.log(_data);
                    
                    let data = {};
                    for (let key in _data) {
                        let sel = `[v-html="${key}"]`
                        // console.log(sel);
                        let tempDoms = root.querySelectorAll(sel)
                        // console.log(key,tempDoms)

                        Object.defineProperty(data,key,{
                            enumerable:true,
                            get(){
                                return _data[key]
                            },
                            set(nv){
                                _data[key] = nv;
                                tempDoms.forEach(element => {
                                    element.innerHTML = _data[key]
                                });
                            }
                        })

                        tempDoms.forEach(element => {
                            element.innerHTML = _data[key]
                        });
                    }
                    
                    for (let key in options.methods) {
                        let sel = `[vOnClick="${key}"]`
                        let tempDoms = root.querySelectorAll(sel)
                        tempDoms.forEach(element=>{
                            element.addEventListener("click",options.methods[key].bind(data) )
                        })
                    }
                    
                    return data
                }
            }
        }
        // 1、构建方法完成应用构建-提供响应式数据-提供响应式的作用容器
        var app = Mvvm.createApp({
            data(){
                return {
                    msg:"测试数据",
                    info:"info",
                    num:1234
                }
            },
            methods:{
                setMsg(){
                    console.log("setMsg",this)
                    this.msg = Math.random();
                }
            }
        })
        
        var vm = app.mount("#app")
        console.log(vm)
    </script>
</body>
</html>